package com.calciominoBeta.Fragment;

import java.util.ArrayList;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.calciomio.app.R;
import com.calciominoBeta.Activity.AQDetailActivity;
import com.calciominoBeta.Activity.db.DbSupport;
import com.calciominoBeta.Activity.db.TableDb;
import com.calciominoBeta.Adapter.Accueil_Adapter;
import com.calciominoBeta.Entity.InfoOj;
import com.calciominoBeta.Util.MyUrl;
import com.calciominoBeta.Util.Sdata;
import com.calciominoBeta.Util.Util;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.model.BaseTask;
import com.telpoo.frame.model.TaskParams;
import com.telpoo.frame.net.BaseNetSupportBeta;
import com.telpoo.frame.net.NetConfig;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.FileSupport;
import com.telpoo.frame.utils.JsonSupport;
import com.telpoo.frame.utils.Mlog;

public class ListNewsFragment extends Fragment {
	private ListView lv_accueil;
	private Accueil_Adapter adapter;
	private View view;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected DisplayImageOptions options;
	private int page = 1;
	public static String url;
	String currentTable = "";
	ProgressBar pbar;
	Handler mHandler = new Handler();
	// private static InterstitialAd interstitial;
	private static AdView adView;
	public static String name;
	public static String email;
	
	public ListNewsFragment() {
	}

	public ListNewsFragment(String url) {
		ListNewsFragment.url = url;
		if (url.equals(MyUrl.ARTICLES))
			currentTable = TableDb.ARCTILE;
		if (url.equals(MyUrl.BREVES))
			currentTable = TableDb.BREVES;
		if (url.equals(MyUrl.HOME))
			currentTable = TableDb.HOME;
		if (url.equals(MyUrl.VIDEO))
			currentTable = TableDb.VIDEO;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_main, null);
		adView = (AdView) view.findViewById(R.id.ad);
		showAdmod();
		Util.checkinternet(getActivity());
		options= new DisplayImageOptions.Builder()
				.cacheInMemory(false).cacheOnDisc(true)
				.bitmapConfig(Config.RGB_565)
				.considerExifParams(false)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.handler(new Handler()).build();
		Util.config(getActivity());
		pbar = (ProgressBar) view.findViewById(R.id.pbar);
		pbar.setVisibility(View.VISIBLE);
		lv_accueil = (ListView) view.findViewById(R.id.lv_accueil);
		adapter = new Accueil_Adapter(getActivity(), imageLoader, options);
		lv_accueil.setAdapter(adapter);

		adapter.setData(DbSupport.getCache(currentTable));

		lv_accueil.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				Accueil_Adapter madapter = (Accueil_Adapter) arg0.getAdapter();

				ArrayList<InfoOj> arrNews = madapter.getFull();
				Sdata.dataLv = arrNews;
				Sdata.position = position;
				startActivity(new Intent(getActivity(), AQDetailActivity.class));

			}
		});

		lv_accueil.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScroll(AbsListView view, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}
		});
		loadDataObjHome();
		return view;
	}

	public static boolean showAdmod() {
		AdRequest adRequest = new AdRequest.Builder()
				.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
				.build();

		adView.loadAd(adRequest);
		return true;
	}

	protected void gotoActivity(Activity activity, Class<?> cls, Bundle b) {
		Intent i = new Intent(activity, cls);
		i.putExtras(b);
		startActivity(i);
	}

	void loadDataObjHome() {

		NetConfig config = new NetConfig.Builder().build();
		BaseNetSupportBeta.getInstance().init(config);

		BaseModel model = new BaseModel() {
			@Override
			public void onSuccess(int taskType, java.util.ArrayList<?> list,
					String msg) {
				Mlog.T("hehe");

				String json = (String) list.get(0);

				try {
					ArrayList<BaseObject> ojmoi = JsonSupport
							.jsonArray2BaseOj(json);

					DbSupport.saveCache(ojmoi, currentTable);

					adapter.setData(Util.convertOj(ojmoi));

				} catch (JSONException e) { // note
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pbar.setVisibility(View.GONE);

			};

			@Override
			public void onFail(int taskType, String msg) {
				pbar.setVisibility(View.VISIBLE);
			};
		};

		BaseTask task = new BaseTask(model, 1) {
			@Override
			protected Boolean doInBackground(TaskParams... params) {
				String json = BaseNetSupportBeta.getInstance().method_GET(url);
				json = FileSupport.fromUTF8String(json);

				ArrayList<String> arr = new ArrayList<String>();

				if (json == null)
					return TASK_FAILED;

				arr.add(json);
				dataReturn = arr;

				return TASK_DONE;

			}
		};
		task.exe();

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
}

}
